&create_image("test1.bmp", 41, -75, qw/KMDBALTI25 KMDBALTI22 KMDBALTI29 MMDO61 KMDLANSD1/);

sub create_image{

local($out_file)=shift @_;

  $n_row=512; 
  $n_col=512;

  open (G,">$out_file");
#  flock(G,2);

## copy image header

  $size =1078+$n_col*$n_row;
  $header[0]=66;
  $header[1]=77;
  $header[2]=$size%256;
  $size=($size-$header[2])/256;
  $header[3]=$size%256;
  $size=($size-$header[3])/256;
  $header[4]=$size%256;
  $size=($size-$header[4])/256;
  $header[5]=$size;
  $header[10]=54;
  $header[11]=4;
  $header[14]=40;
  $header[18]=$n_col%256;
  $header[19]=$n_col/256;
  $header[22]=$n_row%256;
  $header[23]=$n_row/256;
  $header[26]=1;
  $header[28]=8;

  for ($k=0; $k<54; $k++) {
    print G chr($header[$k]);
  }

## create and copy palette

  for ($k=0; $k<256; $k++) {
  
    $red=int(64+$k/2);
    $green=int(64+$k/2);
    $blue=0; #int(64+$k/2);

    $alpha=0;
    print G chr($blue).chr($green).chr($red).chr($alpha);

  }

## create bitmap

my $searchLat = shift @_;
my $searchLong = shift @_;
my @stations = @_;

my @lat;
my @long;
my @temp;
my $i = 1;
for (@stations) {
	print "Downloading " . $i++ . " of " . scalar @stations . "\n";
	my $url = "http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=" . $_;
	my @page = `curl -s $url`;
	my %data = ();
	for my $line (@page) {
	  if ($line =~ m/^\s*<([^>]+)>([^<]+)<\/\1>\s*$/) {
	    $data{$1} = $2;
	  }
	}
	if (defined $data{'latitude'} and
	    defined $data{'longitude'} and
	    defined $data{'temp_c'}) {
	  push @lat, $data{'latitude'};
	  push @long, $data{'longitude'};
	  push @temp, $data{'temp_c'};
	}
}

print "temps = [",join(', ', @temp),"]\n";

@lat = zero(@lat);
@long = zero(@long);
$latmax = max(@lat);
$longmax = max(@long);

if ($latmax > $longmax) {
	@lat = scale($latmax,@lat);
	@long = scale($latmax,@long);
	$searchLat = scale($latmax,$searchLat);
	$searchLong = scale($latmax,$searchLong);
} else {
	@lat = scale($longmax,@lat);
	@long = scale($longmax,@long);
	$searchLat = scale($longmax,$searchLat);
	$searchLong = scale($longmax,$searchLong);
}
@temp = zero(@temp);
my $tempmax = max(@temp)*1.1;
@temp = map {int($_*255/$tempmax)} @temp;

for ($k=0; $k <= $#temp; $k++) {
	$p[$long[$k]][$lat[$k]]=$temp[$k];
	print "$lat[$k]\t$long[$k]\t$temp[$k]\n";
}

if ($searchLong >= 2) {
  $p[($searchLong-2)][$searchLat] = 255;
}
if ($searchLong >= 1) {
  $p[($searchLong-1)][$searchLat] = 255;
}
$p[($searchLong)][$searchLat] = 255;
$p[($searchLong+1)][$searchLat] = 255;
$p[($searchLong+2)][$searchLat] = 255;

if ($searchLat >= 2) {
  $p[$searchLong][($searchLat-2)] = 255;
}
if ($searchLat >= 1) {
  $p[$searchLong][($searchLat-1)] = 255;
}
#$p[$searchLong][($searchLat)] = 255;
$p[$searchLong][($searchLat+1)] = 255;
$p[$searchLong][($searchLat+2)] = 255;

## save bitmap  

  for ($k=0; $k<$n_row; $k++) {
    for ($l=0; $l<$n_col; $l++) {
      $pix=$p[$k][$l]; 
      ## print "$pix\n";
      print G chr($pix);
    }
  }


  close(G);
#  flock(G,8);

}

sub zero {
	my @list = @_;
	my $min = $list[0];
	for (@list) {
		if ($_ < $min) { $min = $_; }
	}
	@list = map { $_ - $min; } @list;
	return @list;
}

sub max {
	my @list = @_;
	my $max = $list[0];
	for (@list) {
		if ($_ > $max) { $max = $_; }
	}
	return $max;
}

sub scale {
	my $max = shift @_;
	$max = $max*1.1 + .01;
	my @list = map { int($_*511/$max); } @_;
	return @list;
}
